iT邦幫忙

2024 iThome 鐵人賽

DAY 7
0
Python

30天零基礎學習Python程式語言系列 第 7

Day 7: 溫度轉換器 + 邏輯運算子

  • 分享至 

  • xImage
  •  

溫度轉換器:

unit = input("請輸入當前的溫度單位(攝氏 C,華氏 F):")
temp = float(input("請輸入溫度:"))

if unit == "C":
    temp = round(9 * temp / 5 + 32)
    print(f"當前的溫度為{temp}度F")
elif unit == "F":
    temp = round((temp-32) * 5 / 9)
    print(f"當前的溫度為{temp}度C")
else:
    print("非法的溫度單位,請重新輸入")

  1. 攝氏轉華氏
    https://ithelp.ithome.com.tw/upload/images/20240912/20169144F8jNS4pH6v.png

  2. 華氏轉攝氏
    https://ithelp.ithome.com.tw/upload/images/20240912/20169144encm7Jh68S.png

  3. 輸入錯誤溫度單位
    https://ithelp.ithome.com.tw/upload/images/20240912/20169144cQJ4ms1b2D.png


邏輯運算子(運算符):
https://ithelp.ithome.com.tw/upload/images/20240912/20169144CemGRLgjk1.jpg

  1. AND(皆true則true)
  2. OR(有true則true)
#AND(皆true則true)
temp = int(input("請輸入現在溫度:"))
if temp > 0 and temp <= 30:
    print("溫度是事宜的")
else:
    print("溫度是不適宜的")

#OR(有true則true)
if temp < 0 or temp >= 30:
    print("溫度是事宜的")
else:
    print("溫度是不適宜的")

  1. NOT
if not temp >= 30: 
print("溫度適宜") 
else: 
print("溫度不適宜") 

*把 not放前面(跟 and or 不太一樣,那兩個是放在中間) not的意思是指只要不符合 temp >= 30 (條件) ,就會判斷為正確,顯示 { 溫度適宜 } ,相反的如果符合就會判斷為錯誤,顯示 { 溫度不適宜 } 這樣輸入 30 會顯示溫度不適宜。
簡單來說,如果一個表達式的結果是 True,那麼使用 not 後的結果就是 False;反之亦然。


上一篇
Day 6: 計算機程式 + 體重轉換器
下一篇
Day 8: 字串方法
系列文
30天零基礎學習Python程式語言21
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言